package com.java2007.zhangyu.restaurant.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Jdbc工具类
 * @author zy
 * @Version
 * @date 2021/01/21
 */
public class JdbcUtils {
    /**
     *  初始化 Druid数据库连接池
     */
    private static DruidDataSource dataSource;

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal();

    static{
        try {
            Properties properties = new Properties();
            //通过反射读取jdbc.properties配置文件中的属性
            InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //从流中加载数据
            properties.load(is);
            //创建数据库连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接池的连接
     * @return 若返回null，表示获取连接失败；若返回其他值，则说明获取成功
     */
    public static Connection getConnection(){
        //从 ThreadLocal 上获取绑定的线程
        Connection connection = threadLocal.get();
        //判断线程上是否有连接connection
        if (null == connection){
            try {
                //当线程上没有connection时，从数据库连接池中获取一个连接
                connection = dataSource.getConnection();
                //把connection对象绑定到本地线程上，供后面的jdbc操作使用
                threadLocal.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    /**
     * 释放资源
     * @param connection
     * @param statement
     * @param resultSet
     */
    public  static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet!=null){
                resultSet.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (connection!=null){
                connection.close();
                threadLocal.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 开启事务
     */
    public static  void begin(){
        //获取数据库连接
        Connection connection = null;
        connection = getConnection();
        try {
            //关闭自动提交，设置为手动管理事务
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public static void commitAndClose() {
        //获取数据库连接
        Connection connection = null;
        connection = getConnection();
        try {
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }
    }

    /**
     * 回滚事务
     */
    public static void rollbackAndClose() {
        //获取数据库连接
        Connection connection = null;
        connection = getConnection();
        try {
            //回滚事务
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }
    }


}
